
$(document).ready(function(){
	
// 假设tableJson是从服务器获取的JSON字符串或者是在代码中定义的字符串
const tableJson = '{  "rows": 5, \
 "cols": 5,  "mergedCells": [{"row": 0, "col": 1, "colSpan": 2}, {"row": 1, "col": 0, "rowSpan": 2}  ], "cells": [  {"row": 0, "col": 0,"content": "Cell 1-1",  "styles": {"border": "1px solid black"}} ]}';
// 解析JSON字符串
const tableData = JSON.parse(tableJson);

function createTableFromJson(tableData) {
  const table = document.createElement('table');
  const { rows, cols, mergedCells, cells } = tableData;

  for (let i = 0; i < rows; i++) {
    const tr = table.insertRow();
    for (let j = 0; j < cols; j++) {
      const cell = tr.insertCell();
      const cellContent = cells.find(c => c.row === i && c.col === j);
      if (cellContent) {
        cell.textContent = cellContent.content;
        for (const [key, value] of Object.entries(cellContent.styles)) {
          cell.style[key] = value;
        }
      }
    }
  }

  mergedCells.forEach(mergeInfo => {
    const cell = table.rows[mergeInfo.row].cells[mergeInfo.col];
    if (mergeInfo.colSpan) cell.colSpan = mergeInfo.colSpan;
    if (mergeInfo.rowSpan) cell.rowSpan = mergeInfo.rowSpan;
  });

  document.getElementById('table-container').appendChild(table);
}

createTableFromJson(tableData);
});
